package com.oig.sys.auth.dao;

import com.oig.sys.auth.entity.TbDepartment;
import com.oig.sys.auth.vo.DepartmentVo;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.util.List;

/**
@author wuxl
*/
public interface ITbDepartmentDao extends JpaRepository<TbDepartment, Integer>, JpaSpecificationExecutor<TbDepartment> {

	TbDepartment getTbDepartmentById(Integer id);

	TbDepartment getByDepartCode(String departCode);

	@Query("select new com.oig.sys.auth.vo.DepartmentVo(d.id,d.tenantId,d.tenantCode,d.tenantName,d.departCode,d.departName,d.masterId," +
			"d.masterName, d.establishTime,d.status, d.parentId,d.parentName, d.createUserName,d.createTime,d.lastUpdateName,d.lastUpdateTime) " +
			"from TbDepartment d where d.id=:id")
	DepartmentVo getVoById(@Param("id") Integer id);


	@Query("select new com.oig.sys.auth.vo.DepartmentVo(d.id,d.tenantId,d.tenantCode,d.tenantName,d.departCode,d.departName,d.masterId," +
			"d.masterName, d.establishTime,d.status, d.parentId,d.parentName) " +
			"from TbDepartment d where d.id in(:idList)")
	List<DepartmentVo> findAllByIds(@Param("idList") List<Integer> idList);


	@Query("select new com.oig.sys.auth.vo.DepartmentVo(d.id,d.tenantId,d.tenantCode,d.tenantName,d.departCode,d.departName,d.masterId," +
			"d.masterName, d.establishTime,d.status, d.parentId,d.parentName, d.createUserName,d.createTime,d.lastUpdateName,d.lastUpdateTime) " +
			"from TbDepartment d where d.tenantId=:tenantId and d.status='04'")
	List<DepartmentVo> findByTenantId(@Param("tenantId") Integer tenantId);


	@Query("from TbDepartment d where d.tenantId=:tenantId and d.id not in(:idList)")
	List<TbDepartment> findTenantNeedCancelDepartment(@Param("idList") List<Integer> idList, @Param("tenantId") Integer tenantId);
}
